<script setup lang="ts">
import { reactive, ref, onMounted, watch } from 'vue'
import { getBookingScheduleRuleAPI, getFindScheduleListAPI } from '@/api/hosp'
import { ruleDataType, responseRuleDataType, workDataType, responseWorkListDataType } from '@/api/hosp/type'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()

const scheduleRuleData = ref({} as ruleDataType)
const total = ref<number>(0)
const queryInfo = reactive<{
  page: number
  limit: number
}>({
  page: 1,
  limit: 6,
})
/** 获取医院可预约排班数据 */
const getBookingScheduleRule = async () => {
  const { data: res }: responseRuleDataType = await getBookingScheduleRuleAPI(
    queryInfo.page,
    queryInfo.limit,
    route.query.code as string,
    route.query.depcode as string,
  )
  if (res.code !== 200) return ElMessage.error(res.message)
  scheduleRuleData.value = res.data
  total.value = res.data.total
}

/** 分页器 */
watch(
  () => queryInfo.page,
  (newValue) => {
    currentChange(newValue)
  },
)

const currentChange = (val: number) => {
  queryInfo.page = val
  getBookingScheduleRule()
}
//  上午排班医生
const findMorningList = ref([] as workDataType[])
// 下午排班医生
const findAfterList = ref([] as workDataType[])
// 选中日期
const active = ref<string>('')
// 是否显示即将放号
const will = ref<boolean>(false)
/** 查看号源 */
const viewSource = async (item: any) => {
  findMorningList.value = []
  findAfterList.value = []
  active.value = item.workDate
  will.value = false
  if (item.status === 1) {
    will.value = true
  }
  if (item.status === 0 && item.availableNumber !== -1) {
    const { data: res }: responseWorkListDataType = await getFindScheduleListAPI(
      route.query.code as string,
      route.query.depcode as string,
      item.workDate,
    )
    if (res.code !== 200) return ElMessage.error(res.message)
    findMorningList.value = res.data.filter((item: any) => {
      return item.workTime === 0
    })
    findAfterList.value = res.data.filter((item: any) => {
      return item.workTime === 1
    })
  }
}
/** 跳转到就诊人信息路由 */
const gotoNextStep = (doc: workDataType) => {
  router.push({
    name: 'RegisterNextStep',
    query: {
      code: route.query.code,
      docId: doc.id,
    },
  })
}

onMounted(() => {
  getBookingScheduleRule()
})
</script>

<template>
  <div class="top">
    <span class="hosname">{{ scheduleRuleData.baseMap?.hosname }}</span>
    <span>|</span>
    <span class="outpatient">{{ scheduleRuleData.baseMap?.bigname }}</span>
    <span class="dot">.</span>
    <span class="hosdepartment">{{ scheduleRuleData.baseMap?.depname }}</span>
  </div>
  <div class="content">
    <h2>{{ scheduleRuleData.baseMap?.workDateString }}</h2>
    <div class="month">
      <div
        class="time"
        :class="{
          stop: item.status === -1 || (item.status === 0 && item.availableNumber === -1),
          run: item.status === 0 && item.availableNumber !== -1,
          beto: item.status === 1,
          active: active === item.workDate,
        }"
        v-for="(item, index) in scheduleRuleData.bookingScheduleList"
        :key="index"
        @click="viewSource(item)"
      >
        <div class="time_top">{{ item.workDate }} {{ item.dayOfWeek }}</div>
        <div class="time_bottom">
          <span v-if="item.status === -1">停止挂号</span>
          <span v-else-if="item.status === 0 && item.availableNumber !== -1">有号({{ item.availableNumber }})</span>
          <span v-else-if="item.status === 0 && item.availableNumber === -1">约满</span>
          <span v-else-if="item.status === 1">即将挂号</span>
          <span v-else>暂无消息</span>
        </div>
      </div>
    </div>
    <el-pagination layout="prev, pager, next" v-model:current-page="queryInfo.page" :total="total" />
  </div>
  <!-- 科室医生 -->
  <div class="bottom">
    <!-- 展示即将放号的时间 -->
    <div class="will" v-if="will">
      <span class="will_day">2023年6月3日</span>
      <span>放号</span>
    </div>
    <!-- 展示医生的时间 -->
    <div class="doctor" v-else>
      <div class="morning">
        <div class="morning_tip">
          <svg-icon name="shangwu" wdith="20px" height="20px" />
          <span>上午号源</span>
        </div>
        <div class="morning_info" v-for="m in findMorningList" :key="m.id">
          <div class="left">
            <div class="left_title">
              <span>{{ m.title }}</span>
              <span>|</span>
              <span>{{ m.docname }}</span>
            </div>
            <div class="left_info">
              <span>{{ m.skill }}</span>
            </div>
          </div>
          <div class="right">
            <p>
              ￥
              <span>{{ m.amount }}</span>
            </p>
            <el-button type="primary" size="default" @click="gotoNextStep(m)">剩余{{ m.availableNumber }}</el-button>
          </div>
        </div>
      </div>
      <div class="afternoon">
        <div class="afternoon_tip">
          <svg-icon name="xiawu" wdith="20px" height="20px" />
          <span>下午号源</span>
        </div>
        <div class="afternoon_info" v-for="a in findAfterList" :key="a.id">
          <div class="left">
            <div class="left_title">
              <span>{{ a.title }}</span>
              <span>|</span>
              <span>{{ a.docname }}</span>
            </div>
            <div class="left_info">
              <span>{{ a.skill }}</span>
            </div>
          </div>
          <div class="right">
            <p>
              ￥
              <span>{{ a.amount }}</span>
            </p>
            <el-button type="primary" size="default" @click="gotoNextStep(a)">剩余{{ a.availableNumber }}</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.top {
  height: 100px;
  display: flex;
  align-items: center;
  color: #7f7f7f;
  span {
    margin: 0 5px;
  }
  .dot {
    transform: translateY(-25%);
  }
}
.content {
  margin: 20px 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  h2 {
    font-weight: bold;
  }
  .month {
    display: flex;
    width: 100%;
    margin: 20px 0;
    .time {
      flex: 1;
      margin: 0 10px;
      color: #7f7f7f;
      font-size: 14px;
      font-family: 'Times New Roman', Times, serif;
      cursor: pointer;
      .time_top {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 35px;
        width: 100%;
      }
      .time_bottom {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 45px;
      }
      &.run {
        border: 1px solid #46cbff;
        .time_top {
          background-color: #a6dcf1;
        }
      }
      &.stop {
        border: 1px solid #d6d6d6;
        .time_top {
          background-color: #cbcbcb;
        }
      }
      &.beto {
        border: 1px solid #a6dcf1;
        .time_top {
          background-color: #ecf6fa;
        }
      }
      &.active {
        border: 2px solid rgb(91, 160, 235);
        border-radius: 2px;
        transform: scale(1.1);
      }
    }
  }
}
.bottom {
  .will {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
    font-weight: bold;
    .will_day {
      color: #46cbff;
      margin-right: 5px;
    }
  }
  .doctor {
    .morning,
    .afternoon {
      margin: 23px 0;
      .morning_tip,
      .afternoon_tip {
        display: flex;
        align-items: center;
        span {
          margin-left: 5px;
          color: #84888b;
        }
      }
      .morning_info,
      .afternoon_info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #cbcbcb;
        .left {
          flex: 1;
          .left_title {
            display: flex;
            margin: 20px 0;
            span {
              color: #46cbff;
              font-weight: bold;
              margin: 0 5px;
            }
          }
          .left_info {
            color: #7f7f7f;
            font-size: 12px;
            margin-bottom: 20px;
          }
        }
        .right {
          flex-shrink: 0;
          display: flex;
          align-items: center;
          margin-right: 30px;
          p {
            margin-right: 30px;
            color: #7f7f7f;
            font-size: 14px;
          }
          .el-button {
            width: 90px;
          }
        }
      }
    }
  }
}
</style>
